﻿using Basic.FormModel.DB;
using Basic.FormModel.PageLayout;
using LinqKit;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.FormDAL.Repository
{
    internal class CustomPageLayoutDAL : BasicDAL<DBCustomPageLayout, long>, ICustomPageLayoutDAL
    {
        public CustomPageLayoutDAL ( IRepository<DBCustomPageLayout> basicDAL ) : base(basicDAL, "page.layout")
        {
        }

        public long Add ( PageLayoutAddDto data )
        {
            DBCustomPageLayout db = data.ConvertMap<PageLayoutAddDto, DBCustomPageLayout>();
            db.Id = IdentityHelper.CreateId();
            this._BasicDAL.Insert(db);
            return db.Id;
        }
        public void SetParentId ( long id, long parentId )
        {
            if ( !this._BasicDAL.Update(a => a.ParentId == parentId, a => a.Id == id) )
            {
                throw new ErrorException("form.page.layout.set.fail");
            }
        }
        public void SaveSort ( Dictionary<long, int> sort )
        {
            ISqlQueue<DBCustomPageLayout> queue = this._BasicDAL.BeginQueue();
            sort.ForEach(c =>
            {
                queue.UpdateOneColumn(a => a.Sort == c.Value, a => a.Id == c.Key);
            });
            _ = queue.Submit();
        }
    }
}
